package com.chennan.work.jiuqi;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STSourceType;

import java.io.*;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author chennan
 * @create 2022-09-14
 */
public class ReFileName {

    private static Map<String,String> map = new ConcurrentHashMap<>();

    public static void main(String[] args) throws IOException {
//        reName(new File("D:\\workspace\\nv_project\\shSZSH\\mete\\shczbzb-new\\__default_tenant__\\web\\query\\data_abnormalanalyse"));
        find(new File("D:\\workspace\\nv_project\\shSZSH\\mete\\shczbzb-new\\__default_tenant__\\web\\query\\data_abnormalanalyse"));
        replace2(new File("D:\\workspace\\nv_project\\shSZSH\\mete\\shczbzb-new\\__default_tenant__\\web\\query\\data_abnormalanalyse"));



        System.out.println(map);
    }

    public static void find(File file) throws IOException {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File file1 : files) {
                if (file1.isDirectory()) {
                    find(file1);
                }else{

                    find1(file1);
                }
            }
        }
    }

    public static void find1(File file) throws IOException {
        StringBuilder sb = new StringBuilder();
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String str = bufferedReader.readLine();
        while (Objects.nonNull(str)){
            sb.append(str);
            str = bufferedReader.readLine();
        }
        bufferedReader.close();
        fileReader.close();
        String string = sb.toString();
        string=string.replace("\t","");
        string=string.replace("\n","");
        JSONObject jsonObject = JSON.parseObject(string);
        String name = jsonObject.getString("name");
        JSONObject content = jsonObject.getJSONObject("content");
        String contentTitle = content.getString("title");
        if(!name.contains("master")){
            if(Objects.nonNull(content)&&Objects.nonNull(content.getJSONArray("groups"))){
                JSONObject group = (JSONObject)content.getJSONArray("groups").get(0);
                JSONObject content1 = group.getJSONObject("content");
                JSONObject jsonAction = (JSONObject)content1.getJSONArray("actions").get(0);
                String action = jsonAction.getString("action");
                if(action.equals("confirmDataAnalyseCheck")){
                    String param = jsonAction.getString("param");
                    String substring = name.substring(name.lastIndexOf("."));
                    map.put(substring,param);
                }

            }
        }

    }

    public static void replace2(File file) throws IOException {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File file1 : files) {
                if (file1.isDirectory()) {
                    replace2(file1);
                }else{

                    replace(file1);
                }
            }
        }
    }
    public static void replace(File file) throws IOException {
        StringBuilder sb = new StringBuilder();
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String str = bufferedReader.readLine();
        while (Objects.nonNull(str)){
            sb.append(str);
            str = bufferedReader.readLine();
        }
        String string2 = sb.toString();
        bufferedReader.close();
        fileReader.close();
        if(sb.toString().contains(".master")){
            String string = sb.toString();
            string=string.replace("\t","");
            string=string.replace("\n","");
            JSONObject jsonObject = JSON.parseObject(string);
            String name = jsonObject.getString("name");
            String substring = name.substring(name.lastIndexOf("."));
            if(map.containsKey(substring)){
                String str1 = map.get(substring);
                string2=string2.replace("ab_sb_ykyw",str1);
                String format = JSONFormatter.format(string2);
                FileUtils.writeStringToFile(file, format, "utf-8");
            }
        }
    }


    public static void reName(File file){
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File file1 : files) {
                if (file1.isDirectory()) {
                    reName(file1);
                }else{
                    reNameFile(file1);
//                    reName1(file1);
                }
            }
        }
        
        
    }


    public static void reNameFile(File file){
        try {
            StringBuilder sb = new StringBuilder();
            FileReader fileReader = new FileReader(file);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            String str = bufferedReader.readLine();
            while (Objects.nonNull(str)){
                sb.append(str);
                str = bufferedReader.readLine();
            }
            bufferedReader.close();
            fileReader.close();
            String string = sb.toString();
            string=string.replace("\t","");
            string=string.replace("\n","");
            JSONObject jsonObject = JSON.parseObject(string);
            String name = jsonObject.getString("name");
            JSONObject content = jsonObject.getJSONObject("content");
            String contentTitle = content.getString("title");
            boolean contains = name.contains(".master");
            if (contains) {
                JSONArray groups = content.getJSONArray("groups");
                for (Object group : groups) {
                    JSONObject group1 = (JSONObject) group;
                    String groupTitle = group1.getString("title");
                    if(groupTitle.equals("未处理")){
                        JSONObject jsonObject1 = JSON.parseObject("{\"param\": \"dg_sb_ykyw\", \"enable\": \"ALL\", \"name\": \"confirmDataAnalyseCheck\", \"action\": \"confirmDataAnalyseCheck\", \"title\": \"核实无误\", \"type\": \"\"}");
                        JSONArray jsonArray = group1.getJSONObject("content").getJSONArray("actions");
                        JSONObject o = (JSONObject)jsonArray.get(0);
                        jsonArray.clear();
                        jsonArray.add(jsonObject1);
                        jsonArray.add(o);
                    }else {
                        JSONObject jsonObject1 = JSON.parseObject("{\"param\": \"dg_sb_ykyw\", \"enable\": \"ALL\", \"name\": \"noConfirm\", \"action\": \"noConfirmDataAnalyseCheck\", \"title\": \"取消核实\", \"type\": \"\"}");
                        JSONArray jsonArray = group1.getJSONObject("content").getJSONArray("actions");
                        JSONObject o = (JSONObject)jsonArray.get(0);
                        jsonArray.clear();
                        jsonArray.add(jsonObject1);
                        jsonArray.add(o);
                    }
                }
                jsonObject.put("title",contentTitle);
                bufferedReader.close();
                fileReader.close();
                String format = JSONFormatter.format(jsonObject.toString());
                FileUtils.writeStringToFile(file, format, "utf-8");
            }


        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
